<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\model\Service;

class ServiceController extends Controller
{
    //增值服务后台管理
    public function index(Request $request)
    {
    	$label = '增值服务管理';

		if($request->ajax())
		{
            //后台用户列表接口
            $page = $request->page ? $request->page : 1;
            $limit = $request->limit ? $request->limit : 10;
            $offset = ($page - 1) * $limit;
            $count = Service::count('id');
    
            $Servicemsg = Service::limit($limit)->offset($offset)->get();

            if (!empty($Servicemsg)) 
            {
                foreach ($Servicemsg as $key => &$value) 
                {
                    $value['create_time'] = date('Y-m-d H:i:s', $value['create_time']);
                    unset($value);
                }
            }
            return $this->return_ajax_data($Servicemsg,'','',$count);
		}
    	return view('admin.service.index',compact('label'));
    }

    //增值服务页添加
    public function serviceAdd(Request $request)
    {
        $data = $request->all();
        $is_show = !empty($data['is_show']) ? $data['is_show'] : 0;
        $create_time = time();

        $save_service = new Service;
        $save_service->titles = $data['titles'];
        $save_service->service_text = $data['service_text'];
        $save_service->is_show = $is_show;
        $save_service->create_time = $create_time;
        if($save_service->save())
        {
            return $this->call_back_status($save_service);
        }
    }

    public function serviceEdit(Request $request)
    {
        $data = $request->all();    
        $update_data = [
                        'titles'=>$data['titles'],
                        'service_text'=>$data['service_text'],
                        ];
        $update_status = Service::where('id','=',$data['id'])->update($update_data);
        return $this->call_back_status($update_status);
    }

    public function isShow(Request $request)
    {
        $data = $request->all();
        $status = Service::where('id','=',$data['id'])->update(['is_show'=>$data['is_show']]);
        return $this->call_back_status($status);
    }

    public function servicedel(Request $request)
    {
        $id = $request->id;
        $status = Service::where('id','=',$id)->delete();
        return $this->call_back_status($status);
    }
}
